<?php

namespace app\admin\model;

use ShidianUtil\Thinkphp\Session\SessionManagerInterface;
use ShidianUtil\Thinkphp\Session\User;
use think\facade\Request;

/**
 * Class UM
 * @package app\admin\model
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class UM extends \app\common\model\User implements User
{

    protected $name = 'User';

    /**
     * 获取UM模型
     * @param $id
     * @return \app\common\model\User|null
     */
    protected static function UM($token): ?User
    {
        return UM::where('token', $token)->find();
    }

    /**
     * 依赖注入的时候获取当前UM模型
     * @param SessionManagerInterface $sessionManager
     * @return \app\common\model\User|null
     */
    public static function __make(SessionManagerInterface $sessionManager)
    {
        $token = Request::header('token', null);
        if (is_null($token)) {
            $token = Request::param('token', null);
        }

        if(is_blank($token)){
            return null;
        }

        return static::UM($token);
    }

    /**
     * 1对1
     * @return \think\model\relation\HasOne
     */
    //    public function one(){
    //        return $this->hasOne( Test1::class, 'Test1表中的键', 'User表中的键');
    //    }

    /**
     * 1对多
     * @return \think\model\relation\HasMany
     */
    //    public function many(){
    //        return $this->hasMany( Test1::class, 'Test1表中的键', 'User表中的键');
    //    }

    /**
     * 多对多
     * @return \think\model\relation\BelongsToMany
     */
    //    public function belongs(){
    //        return $this->belongsToMany( Test1::class, '中间表', '中间表中Test1的键', '中间表中User的键');
    //    }
}
